package com.tanguy.user.utlis.extend

import android.app.Activity
import android.graphics.drawable.Drawable
import android.support.annotation.DrawableRes
import android.support.annotation.IdRes
import android.support.v4.content.ContextCompat
import android.view.View
import android.widget.TextView
import org.jetbrains.anko.backgroundResource
import org.jetbrains.anko.find

/**
 * Created by dab on 2017/10/20 0020 14:07
 */


fun View.setText(@IdRes id: Int, string: CharSequence): TextView {
    val find = find<TextView>(id)
    find.text = string
    return find
}

fun View.setBackground(@IdRes id: Int, @DrawableRes res: Int) {
    find<View>(id).backgroundResource = res
}

fun View.visibility(@IdRes id: Int,visible:Boolean) {
    find<View>(id).visibility = if (visible)View.VISIBLE else View.GONE
}
fun Activity.visibility(@IdRes id: Int,visible:Boolean) {
    find<View>(id).visibility = if (visible)View.VISIBLE else View.GONE
}
fun View.visibility(view: View,visible:Boolean) {
    view.visibility = if (visible)View.VISIBLE else View.GONE
}
fun Activity.visibility(view: View,visible:Boolean) {
    view.visibility = if (visible)View.VISIBLE else View.GONE
}
fun TextView.setDrawable(@DrawableRes left: Int=0,@DrawableRes top: Int=0,@DrawableRes right: Int=0,@DrawableRes bottom: Int=0) {
    fun getDrawable(@DrawableRes id: Int): Drawable? {
        if (id<=0)return null
        val drawable = ContextCompat.getDrawable(this.context,id)
        drawable.setBounds(0, 0, drawable.minimumWidth, drawable.minimumHeight)
        return drawable
    }
    this.setCompoundDrawables(getDrawable(left), getDrawable(top), getDrawable(right), getDrawable(bottom))

}

